<?php
namespace Gy_Library;

use Qscmf\Core\QsListController;

class GyListController extends QsListController{
    /**
     * 添加
     * @param string $url 跳转链接
     */
    public function addPost($url=''){
        if (IS_POST) {
            parent::autoCheckToken();
            $data = I('post.');
            $model = D($this->dbname);
            $r = $model->createAdd($data);
            if($r === false){
                $this->error($model->getError());
            }
            else{
                if($url == '')
                    $this->success(l('add') . l('success'), U('index'));
                else
                    $this->success(l('add') . l('success'), $url);
            }
        }
    }
    /**
     * 编辑
     * @param string $url 跳转链接
     * @throws \Think\Exception
     */
    public function editPost($url=''){
        if (IS_POST) {
            parent::autoCheckToken();
            $m_id = I('post.id');
            $data = I('post.');
            $model = D($this->dbname);
            if(!$m_id){
                E('缺少ID');
            }
            $ent = $model->where(['id'=>$m_id])->find();
            if(!$ent){
                E('访问的内容不存在');
            }
//            foreach ($data as $key => $value){
//                if($key != 'id'){
//                    $ent[$key] = $value;
//                }
//            }
            if($model->createSave($data) === false){
                $this->error($model->getError());
            }
            else{
                if($url == '')
                    $this->success('修改成功', U('index'));
                else
                    $this->success('修改成功', $url);
            }
        }
    }
    /**
     * 保存排序
     */
    public function save(){
        if(IS_POST){
            $data = I('post.');
            $model = D($this->dbname);
            foreach($data['id'] as $k=>$v){
                $save_data['sort'] = $data['sort'][$k];
                $model->where('id=' . $v)->save($save_data);
            }
            $this->success('保存成功');
        }
    }
    /**
     * 禁用
     */
    public function forbid(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要禁用的数据');
        }
        $r = parent::_forbid($ids);
        if($r !== false){
            $this->success('禁用成功');
        }
        else{
            $this->error($this->_getError());
        }
    }
    /**
     * 启用
     */
    public function resume(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要启用的数据');
        }
        $r = parent::_resume($ids);
        if($r !== false){
            $this->success('启用成功');
        }
        else{
            $this->error($this->_getError());
        }

    }
    public function delete(){
        $ids = I('ids');
        if(!$ids){
            $this->error('请选择要删除的数据');
        }
        $r = parent::_del($ids);
        if($r === false){
            $this->error($this->_getError());
        }
        else{
            sysLogs('分类id: ' . $ids . ' 删除');
            $this->success('删除成功', U(MODULE_NAME . '/' . CONTROLLER_NAME . '/index'));
        }
    }
}